#!/bin/bash
dir="`pwd`"
cd "$(dirname "$0")"
home="`pwd`"
cd "${dir}"

noedit="$(echo "$@" | grep -o '\b-q\b')"
if [[ "$1" == "-q" ]]; then shift 1; fi
old="$1"
new="$2"
if [[ "$1" == "-q" ]]; then shift 1; fi

if [ -z "$new" ]; then
  echo "  mkmod <existing-script-name> <new-name>"
  exit 1
fi

found="$(echo "`llbin "$new"`" | grep -i "^$new$" | firstline)"
script="`which "$found"`"
scriptdir="$(dirname "$script")"
if [ -e "$script" ]; then
  1>&2 echo "$new already exists at $script"
  if [[ -z "$noedit" ]]; then
    1>&2 echo "Opening it in an editor..."
  fi
else
  found="$(echo "`llbin "$old"`" | grep -i "^$old$" | firstline)"
  if [ -z "$found" ]; then
    1>&2 echo "No such scriptfile: $old"
    exit 1
  fi
  script="`which "$found"`"
  scriptdir="$(dirname "$script")"
  cp "$script" "$scriptdir/$new"

  if [ ! -t 0 ]; then
    cat > "$scriptdir/$new"
  fi

  echo "\"$script\" => \"$scriptdir/$new\""
fi

if [[ -z "$noedit" ]]; then
  exec edit "$scriptdir/$new"
fi

